{{- if (eq .Site.Params.theme.defaultTheme "light") }}
<script>
    if (document.body.className.includes("dark")) {
        document.body.classList.remove('dark');
        document.documentElement.classList.remove('dark');
        localStorage.setItem("pref-theme", 'light');
    }
</script>

{{ else if (eq .Site.Params.theme.defaultTheme "dark") }}
{{- /* theme is dark */}}
<script>
    if (document.body.className.includes("light")) {
        document.documentElement.classList.add('dark');
        document.body.classList.add('dark');
        localStorage.setItem("pref-theme", 'dark');
    }
</script>

{{- else }}
{{- /* theme is auto */}}
<script>
    if (localStorage.getItem("pref-theme") === "dark") {
        document.documentElement.classList.add('dark');
        document.body.classList.add('dark');
    } else if (localStorage.getItem("pref-theme") === "light") {
        document.body.classList.remove('dark')
        document.documentElement.classList.remove('dark');
    } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
        document.documentElement.classList.add('dark');
        document.body.classList.add('dark');
    }

</script>
{{- end }}

{{- if (not .Site.Params.theme.disableThemeToggle) }}
<script>
    document.getElementById("theme-toggle").addEventListener("click", () => {
        if (document.body.className.includes("dark")) {
            document.body.classList.remove('dark');
            document.documentElement.classList.remove('dark');
            localStorage.setItem("pref-theme", 'light');
        } else {
            document.body.classList.add('dark');
            document.documentElement.classList.add('dark');
            localStorage.setItem("pref-theme", 'dark');
        }
    })

    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
    })

</script>
{{- end }}

{{ if not (.Site.Params.navbar.disableSearch | default false) }}
    <script src="{{ .Site.Params.staticPath }}/js/search.js"></script>
{{ end }}


{{ if or (.Params.mathjax | default false) (.Site.Params.mathjax) }}

{{ "<!-- MathJax -->" | safeHTML }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js" integrity="sha384-M5jmNxKC9EVnuqeMwRHvFuYUE8Hhp0TgBruj/GZRkYtiMrCRgH7yvv5KY+Owi7TW" crossorigin="anonymous"></script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        inlineMath: [['\\(','\\)']],
        displayMath: [['$$','$$'], ['\[','\]']],
        processEscapes: true,
        processEnvironments: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
        TeX: { equationNumbers: { autoNumber: "AMS" },
             extensions: ["AMSmath.js", "AMSsymbols.js"] }
      }
    });
</script>
{{ end }}

{{ if (.Site.Params.cloudinary_cloud_name | default false) }}

{{ "<!-- cloudinary -->" | safeHTML }}
<script src="https://unpkg.com/cloudinary-core@2.13.0/cloudinary-core-shrinkwrap.js" integrity="sha384-0bQduxVhZMs6xfvcPH9osdUIw44hjF8EahHuQBdyN6Rryk8tgyFO80Yz5d14F+5d" crossorigin="anonymous"></script>
<script type="text/javascript">
    var cl = cloudinary.Cloudinary.new({cloud_name: "{{- .Site.Params.cloudinary_cloud_name }}"}); 
    cl.responsive();
</script>

{{ end }}

{{ if (.Site.Params.contact.formspree.enable | default false) }}
<script src="{{ .Site.Params.staticPath }}/js/contact.js"></script>
{{ end }}

{{ if (.Site.Params.customScripts | default false) }}
{{ .Site.Params.customScripts | safeHTML }}
{{ end }}
